package com.shuangdj.databinding.presenter;

import android.databinding.ObservableBoolean;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;

import com.shuangdj.databinding.bean.TestModel;

import java.util.ArrayList;

/**
 * Created by linxiaoke on 17/4/1.
 */
//@InverseBindingMethods({
//        @InverseBindingMethod(
//                type = android.support.v4.widget.SwipeRefreshLayout.class,
//                attribute = "refreshing",
//                event = "refreshingAttrChanged",
//                method = "isRefreshing")})
public class TestPresenter {
    public void onSaveClick(View view, TestModel testModel) {
        testModel.setName("name is changed " + System.currentTimeMillis());
        testModel.setDef("ssssss");
        Log.i("sdj", "DDDDD");
    }

    public int check(TestModel testModel) {
        return View.VISIBLE;
    }

    public static int times = 0;


    public void getData() {
        if (refresh == null) return;
        times++;
        ArrayList<TestModel> testModels = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            TestModel tmp = new TestModel("第" + times + "次init ：" + i);
            testModels.add(tmp);
        }
        refresh.onRefresh(testModels);
    }

    private Refresh refresh;

    public void setRefresh(Refresh refresh) {
        this.refresh = refresh;
    }

    public interface Refresh {
        void onRefresh(ArrayList<TestModel> testModels);
    }

    public final ObservableBoolean swipeRefreshViewRefreshing = new ObservableBoolean(false);


    public void load() {
        swipeRefreshViewRefreshing.set(true);

        // 网络请求
//        ....

        Log.i("sdj", "load");
//        swipeRefreshViewRefreshing.set(false);
    }

    public SwipeRefreshLayout.OnRefreshListener onRefreshListener() {
        return new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // Do something you need
                Log.i("sdj", "onRefresh:" + swipeRefreshViewRefreshing.get());
            }
        };
    }

}
